import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import "@/assets/css/base.css";
import "@/assets/css/el-rest.css";
import subMenu from "qf-sub-menu";
import nprogress from "nprogress";
import "nprogress/nprogress.css";
Vue.use(subMenu);
Vue.use(ElementUI);
Vue.config.productionTip = false;
router.beforeEach((to, from, next) => {
  console.log(from);
  nprogress.start();
  let token = localStorage.getItem("qf-token") || "";
  if (token) {
    if (store.state.sideMenu.length === 0) {
      store.dispatch("GET_MENULIST").then(() => {
        next({ to: to.path });
      });
    } else {
      next();
    }
  } else {
    if (to.path !== "/login") {
      next({ path: "/login" });
    } else {
      next();
    }
  }
});
Vue.directive("has", {
  inserted(el, binding) {
    // console.log(el)
    let btn = localStorage.getItem("permissionBtns") || "[]";
    btn = JSON.parse(btn);
    if (!btn.includes(binding.value)) {
      let ElclassName = el.className;
      el.className = ElclassName + " " + "is-disabled";
      //真正禁用按钮
      el.disabled = true;
    }
  },
});
//全局后置钩子
router.afterEach((to, from) => {
  console.log(from);
  nprogress.done();
  let matched = [...to.matched];
  matched.splice(0, 1);
  store.commit("SET_BREAD", matched);
});
new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app");
